//
//  RTStatusCell.swift
//  Weibo
//
//  Created by Brisinga on 16/3/7.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit
import SnapKit

class RTStatusCell: UITableViewCell {
    
    
    // toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    
    var statusViewModel: RTStatusViewModel?  {
    
        didSet {
        
            OriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            
            self.toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.retweetedText != nil {
            
                retweetStatusView.statusViewModel = statusViewModel
                
                retweetStatusView.hidden = false
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).offset(kMargin).constraint
                })
                
            }else {
                
                retweetStatusView.hidden = true
                
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(OriginalView.snp_bottom).offset(kMargin).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 加载控件
        setupUI()
        
        
    }
    
    
    
    private func setupUI() {
        
        selectionStyle = .None
        
        contentView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        contentView.addSubview(OriginalView)
        
        contentView.addSubview(retweetStatusView)
        
        contentView.addSubview(statusToolBar)
        
        
        
        OriginalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.contentView).offset(kMargin)
            make.leading.trailing.equalTo(self.contentView)
        }
        
        
        
        retweetStatusView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(OriginalView.snp_bottom).offset(kMargin)
            make.leading.trailing.equalTo(OriginalView)
        }
        
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(OriginalView)
            make.height.equalTo(35)
            self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).offset(kMargin).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar)
        }

    }
    
    // 懒加载Original
    private lazy var OriginalView: RTOriginalView = RTOriginalView()
    
    // 懒加载RTRetweetStatusView
    private lazy var retweetStatusView: RTRetweetStatusView = RTRetweetStatusView()
    
    // 懒加载toolBar
    private lazy var statusToolBar: RTStatusToolBar = RTStatusToolBar()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
